{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "importing Jupyter notebook from myItem.ipynb\n"
     ]
    }
   ],
   "source": [
    "import smtplib\n",
    "from email.mime.text import MIMEText\n",
    "from email.mime.image import MIMEImage\n",
    "from email.mime.multipart import MIMEMultipart\n",
    "from email.header import Header\n",
    "import datetime\n",
    "import Ipynb_importer\n",
    "from myItem import myItem\n",
    "\n",
    "class sendMail():\n",
    "    def __init__(self):\n",
    "        # 发送方邮箱\n",
    "        self.from_addr = ''\n",
    "        self.password = ''#邮箱授权码\n",
    "        \n",
    "        # 接收方邮箱\n",
    "        self.to_addr = ''\n",
    "        \n",
    "        # 发信服务器\n",
    "        self.smtp_server = 'smtp.qq.com'\n",
    "            \n",
    "    def cutPriceText(self,CutPriceList):\n",
    "        msg = MIMEMultipart()\n",
    "        email_body=''\n",
    "        totalImageIDHtml=''\n",
    "        #email_body=email_body+'\\n'+\"新增的商品信息: \"+'\\n'\n",
    "        for i in range(len(CutPriceList)):\n",
    "            pnameText=str(i+1)+\". 商品名称: \"+CutPriceList[i].getPname()\n",
    "            priceText=\"价格: ￥\"+str(CutPriceList[i].getPrice())\n",
    "            productURLText=CutPriceList[i].getProductURL()\n",
    "            #updateTimeText=\"更新时间: \"+str(CutPriceList[i].getUpdateTime())\n",
    "            \n",
    "            email_body='<br/><a href=\"' + productURLText + '\">'+pnameText+ '</a><br/><a>￥' + priceText + '</a><br/><br/><br/>'\n",
    "            imageIDHtml='<a href=\"' + productURLText + '\"><img src=\"cid:image'+str(i)+'\"/></a> '+email_body\n",
    "            totalImageIDHtml=totalImageIDHtml+imageIDHtml\n",
    "            \n",
    "            img=newArrivalList[i].getProPic()\n",
    "            msgImage = MIMEImage(img)\n",
    "            imageID='<image'+str(i)+'>'\n",
    "            msgImage.add_header('Content-ID', imageID)\n",
    "            msg.attach(msgImage)\n",
    "        msg.attach(MIMEText(totalImageIDHtml, 'html', 'utf-8'))\n",
    "        return msg\n",
    "        \n",
    "    def newArrivalText(self,newArrivalList):\n",
    "        msg = MIMEMultipart()\n",
    "        email_body=''\n",
    "        totalImageIDHtml=''\n",
    "        #email_body=email_body+'\\n'+\"新增的商品信息: \"+'\\n'\n",
    "        for i in range(len(newArrivalList)):\n",
    "            pnameText=str(i+1)+\". 商品名称: \"+newArrivalList[i].getPname()\n",
    "            priceText=\"价格: ￥\"+str(newArrivalList[i].getPrice())\n",
    "            productURLText=newArrivalList[i].getProductURL()\n",
    "            #updateTimeText=\"更新时间: \"+str(newArrivalList[i].getUpdateTime())\n",
    "            \n",
    "            email_body='<br/><a href=\"' + productURLText + '\">'+pnameText+ '</a><br/><a>￥' + priceText + '</a><br/><br/><br/>'\n",
    "            imageIDHtml='<a href=\"' + productURLText + '\"><img src=\"cid:image'+str(i)+'\"/></a> '+email_body\n",
    "            totalImageIDHtml=totalImageIDHtml+imageIDHtml\n",
    "            \n",
    "            img=newArrivalList[i].getProPic()\n",
    "            msgImage = MIMEImage(img)\n",
    "            imageID='<image'+str(i)+'>'\n",
    "            msgImage.add_header('Content-ID', imageID)\n",
    "            msg.attach(msgImage)\n",
    "        msg.attach(MIMEText(totalImageIDHtml, 'html', 'utf-8'))\n",
    "        return msg\n",
    "    \n",
    "    def sendError(self,email_body,subject):\n",
    "        msg = MIMEText(email_body, 'plain', 'utf-8')\n",
    "        msg['From'] = Header(self.from_addr)\n",
    "        msg['To'] = Header(self.to_addr)\n",
    "        msg['Subject'] = Header(subject)\n",
    "        return msg\n",
    "        \n",
    "    def setText(self,msg,subject):\n",
    "        # 邮件头信息\n",
    "        msg['From'] = Header(self.from_addr)\n",
    "        msg['To'] = Header(self.to_addr)\n",
    "        msg['Subject'] = Header(subject)\n",
    "        return msg \n",
    "        \n",
    "    def send(self,msg):\n",
    "        try:\n",
    "            # 开启发信服务，这里使用的是加密传输\n",
    "            server = smtplib.SMTP_SSL(self.smtp_server)\n",
    "            server.connect(self.smtp_server, 465)\n",
    "            # 登录发信邮箱\n",
    "            server.login(self.from_addr, self.password)\n",
    "            # 发送邮件\n",
    "            server.sendmail(self.from_addr, self.to_addr, msg.as_string())\n",
    "            print('send successfully')\n",
    "        except ValueError as value_err:  #可以写多个捕获异常\n",
    "            print(\"ValueError\"+\"\\n\")\n",
    "        finally:\n",
    "            # 关闭服务器\n",
    "            server.quit()\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
